package com.example.function.project.controller;

import com.example.common.support.AjaxResponse;
import com.example.common.support.CP_GlobalNamingConstant;
import com.example.common.support.CP_OperateLog;
import com.example.function.project.model.one.Project;
import com.example.function.project.model.one.ProjectUserRole;
import com.example.function.project.model.pojo.ProjectUserRoleGroupByProjectPojo;
import com.example.function.project.service.IProjectService;
import com.example.function.project.service.IProjectUserRoleService;
import com.example.function.sys.service.ISysUserService;
import com.example.support.CP_AuthenticationUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * Description: <project模块controller >. <br>
 * <p>
 * <基本的crud>
 * </p>
 * generate time:2021-7-27 18:02:19
 *
 * @author hanqf
 * @version V1.0
 */

@RestController
@Slf4j
@RequestMapping("/proj")
public class ProjectUserRoleController {


    @Resource(name = "projectUserRoleService")
    protected IProjectUserRoleService projectUserRoleService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 查询全部.
     *
     * @return AjaxResponse
     */
    @RequestMapping(value = "/users/all")
    @CP_OperateLog(value = "查询全部项目用户角色", type = CP_GlobalNamingConstant.OPERATE_LIST)
    public AjaxResponse projectUserRoles() {
        final List<ProjectUserRole> projectUserRoleList = projectUserRoleService.findAll();
        return AjaxResponse.success(projectUserRoleList);
    }

    /**
     * 按项目分组查询全部.
     *
     * @return AjaxResponse
     */
    @RequestMapping(value = "/users/gbyp")
    @CP_OperateLog(value = "按项目分组查询全部项目用户角色", type = CP_GlobalNamingConstant.OPERATE_LIST)
    public AjaxResponse projectUserRolesGroupByProject() {
        Long userId = sysUserService.findByLoginName(CP_AuthenticationUtils.getUserLoginName()).getId();
        final List<Long> ids = projectUserRoleService.findProjectIdsByUserId(userId);
        final List<Project> projectList = projectService.findAllByIds(ids.toArray(new Long[ids.size()]));
        return AjaxResponse.success(ProjectUserRoleGroupByProjectPojo.wrapper(projectList));
    }


}

